import {
  getActivity,
  delActivity
} from '../../../utils/api.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {

    closed: "",
    disabled: false,
    getAtives: {
      p: 1,
      type: '',
      token: ""
    },
    delAtives: {
      token: "",
      type: '',
      id: ""
    },
    activityList: [],
    isdata: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(options);
    var type_ = 'getAtives.type';
    that.setData({
      [type_]: options.data
    })
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight * 2
        });
      }
    });
    wx.getStorage({
      key: 'token',
      success: function (res) {
        let getAtivesToken = "getAtives.token";
        let delAtivesToken = "delAtives.token";
        that.setData({
          [getAtivesToken]: res.data,
          [delAtivesToken]: res.data
        })
        that.acquireActivity()
      },
    })

  },
  acquireActivity() {
    getActivity(this.data.getAtives).then(res => {
      if (res.code == 200) {
        this.setData({
          activityList: this.data.activityList.concat(res.datas.list.list)
        })
        if (res.datas.more == false) {
          this.setData({
            isdata: false
          })
        }
      } else {
        this.setData({
          activityList: []
        })
      }
    })
  },
  //活动详情
  listDetail(e) {
    let activities = JSON.stringify(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: './listdetails/listdetails?activities=' + activities,
    })
  },
  //删除活动
  delBtn(e) {
    console.log(e);
    let type = e.currentTarget.dataset.type;
    let delAtivesID = "delAtives.id";
    let delAtivestype = "delAtives.type";
    let getAtivesP = "getAtives.p";

    this.setData({
      [delAtivesID]: e.target.dataset.id,
      activityList: [],
      [getAtivesP]: 1,
      [delAtivestype]: type
    })

    delActivity(this.data.delAtives).then(res => {
      wx.showToast({
        title: res.msg
      })
      if (res.code == 603 || res.code == 602) {
        this.acquireActivity()
      }
    })
  },


  //滚动事件
  bindDownLoad(e) {
    if (this.data.isdata) {
      this.data.getAtives.p += 1;
      this.acquireActivity()
    } else {
      wx.showToast({
        title: '到底了',
        icon: 'none'
      })
    }
  },
  /*
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})